import Vue from 'vue'
import Router from 'vue-router'
import Index from "../components/Index";
import Register from "../components/UserApp/Register";
import Login from "../components/UserApp/Login";
import Oauth_callback from "../components/UserApp/Oauth_callback";
import UserInfo from "../components/Personal/UserInfo";
import UserOrder from "../components/Personal/UserOrder";
import UserSite from "../components/Personal/UserSite";
import UserPass from "../components/Personal/UserPass";
import MyCenter from "../components/Personal/MyCenter";
import TypeAdd from "../components/AdminApp/TypeAdd";
import MyIndex from "../components/AdminApp/MyIndex";
import AdvAdd from "../components/AdminApp/AdvAdd";


Vue.use(Router)

let router = new Router({
   mode: 'history',
   routes: [
      {
         path: '/',
         name: 'Index',
         component: Index
      }, {
         path: '/register',
         name: 'Register',
         component: Register
      }, {
         path: '/login',
         name: 'Login',
         component: Login
      }, {
         path: '/oauth_callback',
         name: 'Oauth_callback',
         component: Oauth_callback
      }, {
         path: '/my_center',
         name: 'MyCenter',
         component: MyCenter,
         redirect: '/user_info',
         meta: {
                  is_login: true
               },
         children: [
            {
               path: '/user_info',
               name: 'UserInfo',
               component: UserInfo,
               meta: {
                  is_login: true
               },
            }, {
               path: '/user_order',
               name: 'UserOrder',
               component: UserOrder,
               meta: {
                  is_login: true
               },
            }, {
               path: '/user_site',
               name: 'UserSite',
               component: UserSite,
               meta: {
                  is_login: true
               },
            }, {
               path: '/user_pass',
               name: 'UserPass',
               component: UserPass,
               meta: {
                  is_login: true
               },
            },
         ]
      },{
         path: '/my_index',
         name: 'MyIndex',
         component: MyIndex,
         redirect: '/type_add',
         meta: {
                  is_login: true
               },
         children: [
            {
               path: '/type_add',
               name: 'TypeAdd',
               component: TypeAdd,
               meta: {
                  is_login: true
               },
            },{
               path: '/adv_add',
               name: 'AdvAdd',
               component: AdvAdd,
               meta: {
                  is_login: true
               },
            },
         ]
      },
   ]
});

router.beforeEach((to, from, next) => {
   if (to.meta.is_login) {
      // 如果有meta.is_login属性, 证明需要登录
      let token = sessionStorage.getItem('token');
      if (token) {
         // 已经登录, 放行
         next()
      } else {
         // 没有登录, 跳转登录
         next({
            name: 'Login',
            query: {
               // to.fullPath 判断用户来自哪一个路由, 登录成功跳回去
               rediect: to.fullPath
            }
         })
      }
   } else {
      // 不需要登录, 放行
      next()
   }
});

export default router
